<?php
class CurriculosController extends AppController {

	var $name = 'Curriculos';

	function index() {
		$this->Curriculo->recursive = 0;
		$this->set('curriculos', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid curriculo', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('curriculo', $this->Curriculo->read(null, $id));
	}

    function adicionar_geral(){
        App::import('Model','TipoEmpregosCurriculo');
        App::import('Model','AreasCurriculo');
        
        $TipoEmpregosCurriculo = new TipoEmpregosCurriculo();
        $AreasCurriculo = new AreasCurriculo();
        
        if(!empty($this->data)){

            //Gravo o curriculo
			$this->Curriculo->create();
            if($this->Curriculo->save($this->data)){
                
                //Gravo os tipos de emprego
                $t_e_data = $this->data['Curriculo']['TipoEmpregoCurriculo']['select'];
                
                $curriculo_id = $this->Curriculo->getID();
                /**
                 * @Todo Podemos mudar isso com um save all*/
                foreach($t_e_data as $k=>$v){
                    
                    $t_e_f = array();
                    
                    $t_e_f['tipo_emprego_id']=$v;
                    $t_e_f['curriculo_id']=$curriculo_id;
                    $t_e_gravar = array('TipoEmpregosCurriculo'=>$t_e_f);
                    $TipoEmpregosCurriculo->create();
                    $TipoEmpregosCurriculo->save($t_e_gravar);
                }
                
                $a_c_data = $this->data['Curriculo']['AreasCurriculo']['area_id'];
                
                /**
                 * @Todo Podemos mudar isso com um save all*/
                foreach($a_c_data as $k=>$v){
                    $a_c_f = array('AreasCurriculo'=>array('area_id'=>$v,'curriculo_id'=>$curriculo_id));
                    
                    $AreasCurriculo->create();
                    $AreasCurriculo->save($a_c_f);
                    
                }
                $this->Session->setFlash(__('Seus Dados Foram Gravados, Adicione Algumas Experiencias Profissionais', true));
                $this->redirect(array('action' => 'adicionar_experiencia',$this->Curriculo->getID()));	
                
                
            }
        }
        
        
        $tipo_empregos = $this->Curriculo->TipoEmprego->Find('list');
		$moedas = $this->Curriculo->Moeda->find('list',array('fields'=>array('Moeda.codigo')));
		$regime_salarios = $this->Curriculo->RegimeSalario->find('list');
		$areas = $this->Curriculo->Area->find('list');
		
        
        $this->set(compact('tipo_empregos','moedas','regime_salarios','areas'));
    }
    
    function adicionar_experiencia($curriculo_id=null){
        
		
		
		if($curriculo_id==null){
			$this->redirect(array('action' => 'adicionar_geral'));	
		}
		else{
			
			$curriculo = $this->Curriculo->find('first',array('conditions'=>array('Curriculo.id'=>$curriculo_id)));
			$tipo_empregos = $this->Curriculo->TipoEmprego->Find('list');
			$moedas = $this->Curriculo->Experiencia->Moeda->find('list',array('fields'=>array('Moeda.codigo')));
			$regime_salarios = $this->Curriculo->Experiencia->RegimeSalario->find('list');
			$areas = $this->Curriculo->Experiencia->Area->find('list');
			
			$paises = $this->Curriculo->Experiencia->Paise->find('list');
			$distritos = $this->Curriculo->Experiencia->Distrito->find('list');
			$provincias = $this->Curriculo->Experiencia->Provincia->find('list');
			
			$empresas = $this->Curriculo->Experiencia->Empresa->find('list');

			$this->set(compact('tipo_empregos','moedas','regime_salarios','areas','paises','distritos','provincias','empresas'));
			$this->set('curriculo',$curriculo);
		}
    }
    
    function adicionar_referencia(){
        
    }
    
    function adicionar_educacao(){
        
    }
    
    function adicionar_habilidade(){
        
    }
    
    function adicionar_upload(){
        
    }
    
	function add() {
		if (!empty($this->data)) {
			$this->Curriculo->create();
			if ($this->Curriculo->save($this->data)) {
				$this->Session->setFlash(__('The curriculo has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The curriculo could not be saved. Please, try again.', true));
			}
		}
		$moedas = $this->Curriculo->Moeda->find('list');
		$regimeSalarios = $this->Curriculo->RegimeSalario->find('list');
		$areas = $this->Curriculo->Area->find('list');
		$tipoEmpregos = $this->Curriculo->TipoEmprego->find('list');
		$this->set(compact('moedas', 'regimeSalarios', 'areas', 'tipoEmpregos'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid curriculo', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Curriculo->save($this->data)) {
				$this->Session->setFlash(__('The curriculo has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The curriculo could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Curriculo->read(null, $id);
		}
		$moedas = $this->Curriculo->Moeda->find('list');
		$regimeSalarios = $this->Curriculo->RegimeSalario->find('list');
		$areas = $this->Curriculo->Area->find('list');
		$tipoEmpregos = $this->Curriculo->TipoEmprego->find('list');
		$this->set(compact('moedas', 'regimeSalarios', 'areas', 'tipoEmpregos'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for curriculo', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Curriculo->delete($id)) {
			$this->Session->setFlash(__('Curriculo deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Curriculo was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
